package com.dofast.bt.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dofast.bt.R;
import com.dofast.bt.base.BaseApplication;
import com.dofast.bt.bean.GoodsBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/12/20.
 */

public class GoodsAdapter extends BaseAdapter {
    private List<GoodsBean> list = new ArrayList<GoodsBean>();

    public GoodsAdapter(List<GoodsBean> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (null == view) {
            view = LayoutInflater.from(BaseApplication.getInstance().getApplicationContext()).inflate(R.layout.item_grid_goods, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        // TODO: 2016/12/22 0022 设置商品图片
        Glide.with(BaseApplication.getInstance().getApplicationContext()).load(list.get(i).getImg_head()).placeholder(R.mipmap.tea).into(holder.ivGood);
        holder.tvName.setText(list.get(i).getName());
        holder.tvPrice.setText("¥" + list.get(i).getPrice() + "");
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.iv_good)
        ImageView ivGood;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_price)
        TextView tvPrice;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
